package com.getpebble.android.common.framework.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.getpebble.android.common.core.trace.Trace;
import java.util.ArrayList;
import java.util.List;

public class PblDynamicAdapter<T> extends BaseAdapter {
    private List<T> mItems = new ArrayList();
    private int mLayoutId = 0;
    private LayoutInflater mLayoutInflater = null;
    private IPblViewBinder mViewBinder = null;

    public interface IPblViewBinder<T, V extends View> {
        boolean onBind(T t, V v, int i);
    }

    public PblDynamicAdapter(Context context, int layoutId) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mLayoutId = layoutId;
    }

    public void add(T item) {
        if (item != null) {
            this.mItems.add(item);
        }
    }

    public void clear() {
        this.mItems.clear();
    }

    public int getCount() {
        return this.mItems.size();
    }

    public T getItem(int position) {
        if (position >= this.mItems.size() || position <= -1) {
            return null;
        }
        return this.mItems.get(position);
    }

    public long getItemId(int position) {
        T item = getItem(position);
        if (item != null) {
            return (long) item.hashCode();
        }
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = this.mLayoutInflater.inflate(this.mLayoutId, parent, false);
        }
        boolean bound = false;
        if (this.mViewBinder != null) {
            bound = this.mViewBinder.onBind(getItem(position), convertView, position);
        }
        if (!bound) {
            Trace.error(PblDynamicAdapter.class.getSimpleName(), "Unable to bind item view, view binder failed!");
        }
        return convertView;
    }

    public void setViewBinder(IPblViewBinder viewBinder) {
        this.mViewBinder = viewBinder;
    }
}
